define(function(require, exports, module)
{
  var $ = require("$");
  var bootbox = require("bootbox");
  var dtUtil = require('datatableutil');
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');
    
    exports.init = function(param)
    {
        //加载表单模板和数据
        var Table = require("table");
        // 查询内容显示
        new Table(
                {
                    attrs :
                    {
                        id : "data_table",
                        isHovered : false,
                        isStriped : true,
                        isBordered : false
                    },
                    renderTo : param.divWrapperId
                   
                });
        // isv信息显示
        var isvinfo_data_table = $('#data_table').dataTable({
                "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
                "bJQueryUI" : false,
                "oLanguage" :
                {
                    "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
                },
                "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
                'bPaginate' : false, // 是否分页
                "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
                "bServerSide" : true, // 指定服务器端分页
                "bDeferRender" : true,
                "bFilter" : true,
                "bSort" : true,
                "bInfo" : true,
                "sAjaxSource" : param.ajaxUrl, 
                "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT,  // 默认分页的每页记录数
                "fnServerData" : function(sSource, aoData, fnCallback, oSettings)
                {
                    
                   // 添加自己的额外参数
                   oSettings.jqXHR = $.ajax(
                   {
                      "dataType" : 'json',
                       "contentType" : "application/json;charset=UTF-8",
                       "type" : "GET",
                       "url" : sSource,
                       "data" : null, // 将请求数据转换为后台要求的JSON格式
                       "success" : function(json)
                       {
                         if(json.flag == '000000'){
                           dtUtil.successCallback(json, aoData, fnCallback);                    
                         } else {
                           oauth2Relogin();                    
                         }
                       }
                  });
                    
                    
                },
                "aoColumns" : [
            {
              "sTitle" : "id",
              "bSortable" : false,
              "mData" : "id",
              "bVisible":false
            },        
            {
              "sTitle" : "名称",
              "bSortable" : false,
              "sWidth" : "30%",
              "mData" : "name"
            },
            {
              "sTitle" : "描述",
              "bSortable" : false,
              "sWidth" : "35%",
              "mData" : "description"
            },
            {
              "sTitle" : "创建时间",
              "bSortable" : false,
              "sWidth" : "15%",
              "mData" : "create_time"
            },
            {
              "sTitle" : "操作",
              "bSortable" : false,
              "mData" : null,
              "mRender" : function(data, status, full)
              {
                var ret = '<div class="btn-group" style="margin-top: 8px;">';
                    ret += '<a href="javascript:void(0);" class="btn icon-reply contrast" title="修改"></a>';
                    ret += '<a href="javascript:void(0);" class="btn icon-rocket contrast" title="权限配置"></a>';
                    ret += '<a href="javascript:void(0);" class="btn icon-eraser contrast" title="用户配置"></a>';
                return ret += '</div>';
              }
            }
                 ],
              "fnDrawCallback": function( oSettings ) {
                  // 表格绘制完成后，去除width属性，使表格可缩放
                  $('#'+oSettings.sTableId).css("width", "");
                }
          });
                    
            //新建ISV按钮
            $("#creatbtn").on("click", function()
            {
              $("#roleform").remove();
              var appForm = require("./roleform");
              appForm.init(param.divWrapperId, null);
            });
            //修改按钮
            $("#data_table tbody").on("click", "tr>td>div>a.icon-reply", function(e)
            {
              $("#roleform").remove();
              var rowNode = this.parentNode.parentNode.parentNode;
              var rowData = isvinfo_data_table.fnGetData(rowNode);
              var isvinforeturnform = require("./rolereturnform");
              isvinforeturnform.init(param.divWrapperId, rowData);
            }); 
            //配置权限
            $("#data_table tbody").on("click", "tr>td>div>a.icon-rocket", function(e)
            {
              $("#roleform").remove();
              $("#creatbtn").css('display', 'none');
              $("#roleMenubtn").css('display', 'inline');
              $("#backbtn").css('display', 'inline');
              var rowNode = this.parentNode.parentNode.parentNode;
              var rowData = isvinfo_data_table.fnGetData(rowNode);
              var isvinforeturnform = require("./rolemenu");
              isvinforeturnform.init(param.divWrapperId, rowData);
            }); 
          //配置用户
            $("#data_table tbody").on("click", "tr>td>div>a.icon-eraser", function(e)
            {
              $("#roleform").remove();
              $("#creatbtn").css('display', 'none');
              $("#roleUserbtn").css('display', 'inline');
              $("#backbtn").css('display', 'inline');
              var rowNode = this.parentNode.parentNode.parentNode;
              var rowData = isvinfo_data_table.fnGetData(rowNode);
              var isvinforeturnform = require("./roleuser");
              isvinforeturnform.init(param.divWrapperId, rowData);
            }); 
        
    };
});

